require "Net/send_Msg"
require "Net/TitleHandler"
require 'View/UIBase/UIBase'

UIInvasionTeam = UIBase.New(PanelNames.UIInvasionTeam);

function UIInvasionTeam.Init(func)
    UIBase.Init(UIInvasionTeam, func);
end

--定义控件
function UIInvasionTeam:OnCreate()
    -------------table
    self.teamItems = {}; --上阵英雄组件
    ---@param partnerIdToItems table<string,GComponent>--根据英雄id索引对应的列表item
    self.partnerIdToItems = {};
    -------------GController
    local heroListCom = self:GetChild("Com_DefaultTeamSelect");--英雄列表组件
    -------------GList
    self.dynamicList = heroListCom:GetChild("List_itemGrid");--英雄列表
    -------------GController
    self.tabTypeCtrl = heroListCom:GetController("ctrlTab");--英雄类型控制器
    -------------GButton
    self.tipsBtn = self:GetChild("help");
    self.gotoBattleBtn = heroListCom:GetChild("Btn_Sure");--进入战斗
    self.blackBtn = heroListCom:GetChild("Btn_BackButton");--返回按钮
    self.recommendBtn=self:GetChild("btnRecommend")
    self.skillBtn=self:GetChild("skill")
    self.skillNameText=self:GetChild("text")
    self.skillDescText=self:GetChild("Text_skillInfo")
    self.skillCtrl=self:GetController("skill")
    -------------init
    self.dynamicList.itemRenderer = function(index, obj)
        self:setData(index + 1, obj);
    end
    self.dynamicList:SetVirtual()
    for i = 1, InvasionManager.HERO_FORMATION_COUNT do
        --选择上线的英雄组件
        local item = self:GetChild("Btn_TeamPos" .. i);
        table.insert(self.teamItems, item);
    end
    self:addEvent();
    self.mainReward = self:GetChild("mainReward")
    logicMgr.FuncOpenManager.refreshMainReward(self.mainReward,Config.FuncOpenType.InvasionBattle)
end
function UIInvasionTeam:OnEnable()
    self:initData()
    logicMgr.InvasionManager.setInvasionShowHeroFormation(InvasionHandler.GetMyMember())
    self.tabTypeCtrl.selectedIndex = 0;--每次进入后重置选择英雄全部显示
    self.allHeros = logicMgr.InvasionManager.getInvasionHeroes();
    self:onClickType()
    self:refreshFormation()
    AudioManager.Play(31)
end

function UIInvasionTeam:OnDisable()

end

function UIInvasionTeam:OnDestroy()

end
-------------------------------function--------------------------------------
function UIInvasionTeam:addEvent()
    --btnChangeFormation.onClick:Add(function()
    --    self.ShowSelectFom(1);
    --end);
    self.tabTypeCtrl.onChanged:Add(function()
        --切换职业选择
        self:onClickType()
    end)
    self:RegisterOnClick(self.blackBtn, self.onCloseClick);
    self:RegisterOnClick(self.gotoBattleBtn, self.onClickGoBattle);
    self:RegisterOnClick(self.recommendBtn, self.onClickRecommend);
    self:RegisterOnClick(self.tipsBtn, self.onClickTip);

    self:RegisterEvent(Config.EventType.InvasionStart,self.StartInvasion);
    self:RegisterEvent(Config.EventType.InvasionRefreshSetInvasion,self.RefreshInvasion)
end
function UIInvasionTeam:removeEvent()
    self.tabTypeCtrl.onChanged:Clear();
end

--初始化
function UIInvasionTeam:initData()
    self.battleHero = {}--上阵的英雄
    self.allHeros = {}--全部英雄
    self.showHeros = {}--显示的英雄
end

function UIInvasionTeam:setData(index, obj)
    local itemValue = self.showHeros[index];
    local go = obj;

    go.onClick:Clear()
    go.onClick:Add(function()
        self:onClickHero(itemValue, go)
    end)
    self.partnerIdToItems[itemValue.partnerId] = go;
    local charactor = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    if charactor ~= nil then
        --初始英雄信息
        if itemValue then
            charactor.initData(go:GetChild("heroItem"), itemValue, nil, self.formationType);
        end
        local levelText=obj:GetChild("heroItem"):GetChild("level")
        self:SetLevel(levelText)
    end
    local isSelected= logicMgr.InvasionManager.checkInvasionShowHeroFormationToId(itemValue.partnerModelId) ~= 0;
    self:SetHeroItemState(go,isSelected)
end
function UIInvasionTeam:SetLevel(go)
    local level=dataTable.getGlobalInt(487)
    go.text=string.format(getLanguage("InvasionLevel"), level)
end
function UIInvasionTeam:SetHeroItemState(go,isSelected)
    go.selected =isSelected
    local selectText=go:GetChild("heroItem"):GetChild("Text_HadSelect")
    selectText.visible=false
    local grayCtrl=go:GetChild("heroItem"):GetController("HadSelect")
    if isSelected then
        grayCtrl.selectedIndex=1
    else
        grayCtrl.selectedIndex=0
    end
end
--onClick--按钮响应
function UIInvasionTeam:onClickHero(heroData, go)
    local state = logicMgr.InvasionManager.setInvasionShowHeroState(heroData.partnerModelId);
    if state ~= nil then
        local isSelected=state == logicMgr.InvasionManager.BATTLE_STATE.ADD
        self:SetHeroItemState(go,isSelected)
        self:refreshFormation()
    end
end
function UIInvasionTeam:RefreshActivateSkill()
    local recommendSkill={}
    local flag
    if not logicMgr.InvasionManager.IsInvasionFull() then
        flag=0
    else
        recommendSkill=logicMgr.InvasionManager.GetCurrentSkill()
        if recommendSkill then
            flag=1
        else
            flag=2
        end
    end
    if flag==0 then
        logicMgr.InvasionManager.setSkillBtn2(self.skillBtn)
        self.skillCtrl.selectedIndex=0
    elseif flag==1 then
        self.skillNameText.text=getLanguage(recommendSkill.skill.f_Name)
        self.skillDescText.text=getLanguage(recommendSkill.skill.f_Describe)
        logicMgr.InvasionManager.setSkillBtn2(self.skillBtn,{icon=recommendSkill.skill.icon})
        self.skillCtrl.selectedIndex=1
    else
        logicMgr.InvasionManager.setSkillBtn2(self.skillBtn)
        self.skillCtrl.selectedIndex=2
    end
end
function UIInvasionTeam:onClickType()
    self.curTabType = self.tabTypeCtrl.selectedIndex;
    self.showHeros = {}
    for i, hero in pairs(self.allHeros) do
        local isShow = false;
        if self.curTabType == 0 then
            isShow = true;
        elseif hero.type == self.curTabType then
            isShow = true;
        end
        if isShow == true then
            table.insert(self.showHeros, hero);
        end
    end
    --self.dynamicList.itemRenderer = function(index, obj)
    --    self:setData(index + 1, obj);
    --end
    local num= #self.showHeros;
    self.dynamicList.numItems=0
    self.dynamicList.numItems =num

end
function UIInvasionTeam:onClickRecommend()
    showUI(PanelNames.UITeamRecommend)
end

function UIInvasionTeam:onClickTip()
    logicMgr.UIShowManager.ShowCommonTips("InvasionRulesTitle", "InvasionRules", "FunctionStory310")
end

function UIInvasionTeam:RefreshInvasion(heroes)

    logicMgr.InvasionManager.setAllInvasionBattleHero(heroes)
    self.allHeros = logicMgr.InvasionManager.getInvasionHeroes()
    self:onClickType()
    self:refreshFormation()
end

function UIInvasionTeam:onClickGoBattle()
    local teamData={}
    for i,v in ipairs(logicMgr.InvasionManager.battleHero)  do
        if v.partnerId ~= nil then 
            local item = {}
            item.site = i
            item.partnerId = v.partnerId
            table.insert(teamData, item)
        end
    end
    if #teamData>0 then
        InvasionHandler.ReqModifyHeroTeamBoss(teamData)
    else
        UISysTips.AddMsg(getLanguage("ErrorCode_3404"))
    end
end

function UIInvasionTeam:StartInvasion()
    self:onCloseClick()
    if not isOpenUI(PanelNames.UIInvasionBattle)then
        showUI(PanelNames.UIInvasionBattle)
    end
end
--刷新上阵英雄显
function UIInvasionTeam:refreshFormation()
    local formation = logicMgr.InvasionManager.getInvasionBattleFormation()
    logicMgr.HeroManager.SetMyHeroSkin(formation)
    local ctrl = CtrlManager.GetCtrl(PanelNames.HeadRectBItem);
    for i = 1, table.getn(formation) do
        local pData = formation[i];
        local obj = self.teamItems[i];
        local ctrlShow = obj:GetController("state");
        ctrlShow.selectedIndex = 0;
        if pData.partnerId ~= nil then
            ctrlShow.selectedIndex = 1;
            pData.notShowNum = true;
            pData.halfIcon = logicMgr.HeroManager.GetHeroBIcon(pData.partnerModelId)
            if ctrl then
                ctrl.initData(obj:GetChild("hero"), pData);
                local levelText=obj:GetChild("hero"):GetChild("level")
                self:SetLevel(levelText)
            end
        end
        obj.onClick:Clear()
        obj.onClick:Add(function()
            InvasionManager.SetHeroDown(i)
            self:refreshFormation()
            self:onClickType()
        end)
    end
    self:RefreshActivateSkill()
end

---关闭页面
function UIInvasionTeam:onCloseClick()
    closeUI(PanelNames.UIInvasionTeam);
    AudioManager.Play(7)
end

---关闭页面
function UIInvasionTeam:onCloseGoBattle()
    closeUI(PanelNames.UIInvasionBattle);
end
return UIInvasionTeam